package com.daocao.yfb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.daocao.yfb.R;
import com.daocao.yfb.adapter.ReceiveAccountAdapter;
import com.daocao.yfb.adapter.ReceiveAccountControlAdapter;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.RelateAddBean;
import com.daocao.yfb.api.bean.RelateDeleteBean;
import com.daocao.yfb.api.model.RelateListModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.util.Dialog;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.util.UI;
import com.daocao.yfb.view.V19FrameLayout;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ReceiveAccountActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_logout)
    TextView btnLogout;
    @BindView(R.id.edit_account)
    EditText editAccount;
    @BindView(R.id.btn_add)
    TextView btnAdd;

    private Unbinder unbiner;
    private ReceiveAccountControlAdapter adapter;
    private String selectId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_account_control);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        editAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(UI.toString(editAccount))) {
                    btnAdd.setVisibility(View.GONE);
                } else {
                    btnAdd.setVisibility(View.VISIBLE);
                }
            }
        });

        adapter = new ReceiveAccountControlAdapter(this);
        adapter.setOnChooseListener(new ReceiveAccountControlAdapter.OnChooseListener() {
            @Override
            public void onClick(String id) {
                if (selectId.contains("," + id)) {
                    selectId = selectId.replace("," + id, "");
                } else {
                    selectId = selectId + "," + id;
                }
            }
        });
        list.setAdapter(adapter);
        getAccount();
    }

    public void getAccount() {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        Api.api_service.getRelateLists().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });

    }

    private void setData(List<RelateListModel.DatasBean> datas) {
        adapter.update(datas);
    }

    public void getAddAccount(String account) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        RelateAddBean bean = new RelateAddBean();
        bean.account = account;
        Api.api_service.getRelateAdd(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    editAccount.setText("");
                    ToastUtil.makeText(this, "添加成功");
                    getAccount();
                });

    }

    public void getDeleteAccount(String relate_ids) {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        if (relate_ids.startsWith(",")) {
            relate_ids = relate_ids.replaceFirst(",", "");
        }
        RelateDeleteBean bean = new RelateDeleteBean();
        bean.relate_ids = relate_ids;
        Api.api_service.getRelateDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    selectId = "";
                    editAccount.setText("");
                    txtRight.setText("编辑");
                    adapter.setShowCheck(false);
                    txtRight.setTextColor(0xff5177fe);
                    ToastUtil.makeText(this, "删除成功");
                    getAccount();
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_add, R.id.btn_logout, R.id.txt_right})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                if (UI.toString(txtRight).equals("编辑")) {
                    adapter.setShowCheck(true);
                    txtRight.setText("删除");
                    txtRight.setTextColor(0xFFF05454);
                } else {
                    if (TextUtils.isEmpty(selectId)) {
                        return;
                    }
                    Dialog.showTipsDialog(this, "提示", "确定要删除吗?", new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            getDeleteAccount(selectId);
                        }

                        @Override
                        public void cancel() {

                        }
                    });

                }
                break;
            case R.id.btn_add:
                getAddAccount(UI.toString(editAccount));
                break;
            case R.id.btn_logout:
                Intent intent=new Intent(this,WebViewActivity.class);
                intent.putExtra("url","http://uc.yunlianhui.cn/index.php/Register/registerOne/rcm_id/%E5%8F%B6%E9%93%B6%E5%AE%89.html");
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }
}
